import { message } from "antd";
import { useState } from "react";
import SelectActions from "../../../../../common/components/SelectAction";
import ActionMap from "../../ActionMap";
import { updateOrderStatus } from "../../utils/orderStatus";
import { checkOrder, countOrderSum } from "../../utils/checkOrderConfig";

//默认样式
const defaultStyle = {
	display: 'inline-block',
	margin: '0 8px 8px 0'
}

const Actions = props => {
	const { type, searchForm, selectKeys, operationPage, orderList, handleOk, handleSavaPrint } = props;
	const [modalName, setModalName] = useState();
	const [item, setItem] = useState({});

	//智能操作，不需要勾选
	const handleAutoOpera = props => {
		const { scene, modalValue, item = {} } = props;
		setItem({
			scene,
			...item
		});
		setModalName(modalValue);
	};

	//点击批量更新状态
	const handleUpdateOrderStatus = props => {
		const { scene, isModal, modalValue, isCheck = true, item = {} } = props;
		//这里需要判断是否选择了数据
		if (selectKeys.length > 0) {
			let checkResult = false,
				countList = [];
			if (isCheck) {
				checkResult = checkOrder(scene, selectKeys, orderList);
				countList = countOrderSum(scene, selectKeys, orderList);
			}

			if (checkResult || !isCheck) {
				if (isModal) {
					setItem({
						scene,
						countList,
						...item
					});
					setModalName(modalValue);
				} else {
					const items = {
						selectKeys,
						orderList
					};
					updateOrderStatus(scene, items, afterUpdate);
				}
			}
		} else {
			message.info("请选择订单");
		}
	};

	//更新状态之后的函数
	const afterUpdate = () => {
		//这里处理成功的逻辑
		handleOk();
	};

	//弹框参数
	const ModalProps = {
		type,
		item,
		searchForm,
		operationPage,
		selectKeys,
		orderList,
		handleSavaPrint,
		handleOk(updateType, data) {
			setModalName();
			handleOk(updateType, data);
		},
		handleCancel() {
			setModalName();
			setItem({});
		}
	};

	//导出配置项
	const exportOptions = [
		{
			label: "按勾选订单导出",
			action() {
				handleUpdateOrderStatus({
					isModal: true,
					isCheck: false,
					item: { isBatch: true, taskType: 2004 },
					modalValue: 'ExportTelCodeModal'
				});
			}
		},
		{
			label: "按查询订单导出",
			action() {
				if (orderList.length) {
					handleAutoOpera({
						item: { isBatch: false, taskType: 2004 },
						modalValue: 'ExportTelCodeModal'
					});
				} else {
					message.info("暂无可导出的数据！");
				}
			}
		},
		{
			label: "维护导出表字段信息",
			action() {
				// handleUpdateOrderStatus({ isModal: true, isCheck: false, modalValue: 'ExportOrderExcelModal' });
				setModalName('ExportOrderExcelModal');
			}
		}
	]


	//修改标记的配置项
	const updateOptions = [
		{
			label: "设置标签",
			action() {
				handleUpdateOrderStatus({
					isModal: true,
					isCheck: false,
					modalValue: 'SetOrderLabelModal'
				});
			}
		},
		{
			label: "修改订单备注",
			action() {
				handleUpdateOrderStatus({
					isModal: true,
					isCheck: false,
					modalValue: 'SetOrderNothsModal',
					item: { remarkType: "orderRemark", isBatch: true }
				});
			}
		},
		{
			label: "修改卖家备注",
			action() {
				handleUpdateOrderStatus({
					isModal: true,
					isCheck: false,
					modalValue: 'SetOrderNothsModal',
					item: { remarkType: "sellerMessage", isBatch: true }
				});
			}
		},
	]

	const actionArr = [
		{
			name: '打印快递单', type: 'primary', action: ()=>{ handleUpdateOrderStatus({ isModal: true, isCheck: false, modalValue: 'BatchPrint' })}
		},
		{ name: '修改/标记', options: updateOptions },
		{ name: '导出订单', options: exportOptions },
	]

	return (
		<>
			{modalName && <ActionMap modalName={modalName} ModalProps={ModalProps} />}
			{
				actionArr.map(v => {
					return (
						<SelectActions
							style={defaultStyle}
							key={v.name}
							name={v.name}
							type={v.type}
							options={v.options || []}
							onClick={v.action}
						/>
					)
				})
			}
		</>
	);
};
export default Actions;
